﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _02CardLib
{
    public class Deck
    {
        public Card[] cards;

        public Deck()                                                                                                   
        {
            cards = new Card[52];
            for(int suitVal = 0; suitVal < 4; suitVal++)
            {
                for(int rankVal = 0; rankVal < 4; rankVal++)
                {
                    cards[suitVal * 4 + rankVal] = new Card((Suit)suitVal, (Rank)rankVal);
                }
            }
        }

        public Card GetCard(int numSuit)
        {
            if (numSuit >= 0 && numSuit <= 51)
            {
                return cards[numSuit];
            }
            else
            {
                throw (new ArgumentOutOfRangeException("Suit: ", numSuit, "\nOut of range"));
            }
        }

        public void Shuffle()
        {
            //利用临时数组和Random类随机数
            var tempCards = new Card[52];
            bool[] assigns = new bool[52];

            Random sourceGen = new Random();

            for(int i = 0; i < 52; i++)
            {
                bool foundCard = false;
                int nextDeck = 0;

                while(foundCard  == false)
                {
                    nextDeck = sourceGen.Next(52);
                    if (assigns[nextDeck] == false)
                        foundCard = true;
                }
                assigns[nextDeck] = true;
                tempCards[nextDeck] = cards[i];

            }
            //将temp数组复制到原来的数组中
            assigns.CopyTo(cards, 0);

        }
    }
}
